﻿using System.Collections.Generic;
using System.IO;
using System.Linq;
using GC_MU.BusinessObjects.Content;
using GC_MU.Core.Constants.Text;
using GC_MU.Core.Extensions.IO;
using GC_MU.Domain.Abstractions.Interfaces.Services;

namespace GC_MU.Domain.Services
{
	public class StaticResourcesService : IStaticResourcesService
	{
		#region IStaticResourcesService Members

		public IEnumerable<Image> GetImages(string folderPath, string serverFolderPath, IEnumerable<string> imageExtensions)
		{
			if (string.IsNullOrWhiteSpace(folderPath) || string.IsNullOrWhiteSpace(serverFolderPath))
				return new List<Image>();

			var directoryInfo = new DirectoryInfo(serverFolderPath);
			var files = directoryInfo.GetFilesByExtensions(imageExtensions) ?? new List<string>();

			return files
				.Select(file => new Image
				{
					Url = GetValidFileUrl(folderPath, file)
				})
				.ToList();
		}

		public string GetValidFileUrl(string folderPath, string fileName)
		{
			if (string.IsNullOrWhiteSpace(folderPath) || string.IsNullOrWhiteSpace(fileName))
				return string.Empty;

			var correctPath = folderPath.Replace(Symbols.Tilde, string.Empty);

			return string.Concat(correctPath, fileName).Replace(Symbols.Backslash, Symbols.Slash);
		}

		#endregion
	}
}
